function selectVal(id){
	var options = document.all[id].options;
	for(var i=options.length-1;i>=0;i--){
		if(options[i].selected==true){
			return options[i].text;
		}
	}
}
function selectValFn(id,fn){
	var options = document.all[id].options;
	for(var i=options.length-1;i>=0;i--){
		if(options[i].selected==true){
			fn(options[i].value)
		}
	}
}
function selectClear(id){
	var options = document.all[id].options;
	for(var i=options.length-1;i>=0;i--){
		options.remove(i);
	}
}
function provinceSelect(){
	selectClear('citys');
	selectClear('citys2');
	var i;
	var province = selectVal('province'),city_name,city_code;
	for(i=1; i<citys[province].length; i++){
		for(var key in citys[province][i]){
			city_name=citys[province][i][key][0]["cityname"];
			city_code=citys[province][i][key][0]["cityid"];
			document.getElementById("citys2").add(addOption(city_code,city_name));
		}
	}
	for(var key in citys[province][1]){
		for(i=1; i<citys[province][1][key].length; i++){
			city_name=citys[province][1][key][i]["cityname"];
			city_code=citys[province][1][key][i]["cityid"];
			document.getElementById("citys").add(addOption(city_code,city_name));
		}
	}
}
function citySelect(){
	selectClear('citys');
	var i;
	var province = selectVal('province'),cityid=selectVal('citys2'),city_name,city_code;
	var city_name,city_code;
	for(i=1; i<citys[province].length; i++){
		for(var key in citys[province][i]){
			city_name=citys[province][i][key][0]["cityname"];
			if(city_name==cityid){
				for(var j=1; j<citys[province][i][key].length; j++){
					city_name=citys[province][i][key][j]["cityname"];
					city_code=citys[province][i][key][j]["cityid"];
					document.getElementById("citys").add(addOption(city_code,city_name));
				}
			}
		}
	}
}
function showID(){
	var citystr1=selectVal('province'),citystr2=selectVal('citys2'),citystr3=selectVal('citys');
	var citystr="";
	if(citystr1!=""){
		citystr+=(citystr!="")?" "+citystr1:citystr1;
	}
	if(citystr2!=""){
		citystr+=(citystr!="")?" "+citystr2:citystr2;
	}
	if(citystr3!=""){
		citystr+=(citystr!="")?" "+citystr3:citystr3;
	}
	
	var baseArray=new Array(7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2);
	var reArray=new Array('1','0','X','9','8','7','6','5','4','3','2');
	var strArray1=new Array(1,3,5,7);
	var strArray2=new Array(2,4,6,8);
	var resultArray=new Array();
	
	var i,cityid="",ok=1;
	selectValFn('citys',function(v){
		cityid=v;
		ok=0;
	});
	if(ok){
		selectValFn('citys2',function(v){
			cityid=v;
			ok=0;
		});
	}
	if(ok){
		selectValFn('province',function(v){
			cityid=v;
			ok=0;
		});
	}
	if(cityid==""){
		alert("请选择城市！");
		return;
	}
	
	var years="1900",months="01",days="01";
	selectValFn('years',function(v){
		years=v;
	});
	selectValFn('months',function(v){
		months=v;
		if(months<10) months="0"+String(months)
	});
	selectValFn('days',function(v){
		days=v;
		if(days<10) days="0"+String(days)
	});
	var nums=1;
	selectValFn('nums',function(v){
		nums=v;
	});
	var sex,sexStr='';
	var sexs = document.getElementsByName("sexs");
	for(var i=0;i<sexs.length;i++){
		if(sexs[i].checked==true) sex=sexs[i].value;sexStr=sexs[i].title;
	}
	var str="",str1="",str2="",str3,result,isok;
	str=String(cityid)+String(years)+String(months)+String(days);
	var m;
	for(m=0;m<nums;){
		str3="";
		str1=String(parseInt(Math.random()*3))+String(parseInt(Math.random()*10));
		if(sex==1){
			str2=String(strArray1[parseInt(Math.random()*4)]);
		}
		else{
			str2=String(strArray2[parseInt(Math.random()*4)]);
		}
		str3=str+str1+str2;
		result=0;
		for(var i=0;i<17;i++){
			result=result+parseInt(str3.substr(i,1))*baseArray[i];
		}
		result %= 11;
		str3=str3+reArray[result];
		isok=1;
		for(var k=0;k<resultArray.length;k++){
			if(resultArray[k]==str3){
				isok=0;
				break;
			}
		}
		if(isok){
			resultArray[m]=str3;
			m++;
		}
	}
	str="";
	var date = new Date(),age = getAge(years+'-'+months+'-'+days+' 12:00:00',date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate()+' 12:00:00');
	for(var i=0;i<resultArray.length;i++){
		str=str+'<div>'+sexStr+' <span style="color:red">'+years+"年"+months+"月"+days+'日</span> '+citystr+' <span style="color:red"> '+resultArray[i]+' </span><span> '+age+'</span></div>';
	}
	document.getElementById("showID").innerHTML=str;
}
function showprovinces(){
	var provinces_name,provinces_code;
	for(var i=0; i<provinces.length; i++) {
		provinces_name=provinces[i];
		provinces_code=citys[provinces_name][0]["cityid"];
		document.getElementById("province").add(addOption(provinces_code,provinces_name));
	}
}
function addOption(val,text,checked){
	var opt = document.createElement("OPTION");
	opt.value = val;opt.text = text;
	if(checked){opt.selected=true;}
	return opt;
}
function showdate(){
	var i;
	YEAR = (YEAR&&YEAR>=1900&&YEAR<=2050)?YEAR:1989;
	MONTH = (MONTH&&MONTH>=1&&MONTH<=12)?MONTH:5;
	for(i=1900; i<2050; i++){
		document.getElementById("years").add(addOption(i,i,i==YEAR));
	}
	for(i=1; i<13; i++){
		document.getElementById("months").add(addOption(i,i,i==MONTH));
	}
	for(i=1; i<32; i++){
		document.getElementById("days").add(addOption(i,i));
	}
}
function showsex(){
	var sexs = document.getElementsByName("sexs");
	for(var i=0;i<sexs.length;i++){
		sexs[i].checked=(sexs[i].value==1)?true:false;
	}
}
function shownum(){
	for( var i=1 ; i<11 ; i++){
		document.getElementById("nums").add(addOption(i,i));
	}
}
function yearSelect(){
	
}
function monthSelect(){
	
}
function getAge(beginStr, endStr) {
	var reg = new RegExp(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})(\s)(\d{1,2})(:)(\d{1,2})(:{0,1})(\d{0,2})$/);
	var beginArr = beginStr.match(reg),endArr = endStr.match(reg);
	console.log(beginArr,endArr);
	var days = 0,month = 0,year = 0;
	days = endArr[4] - beginArr[4];
	if (days < 0) { month = -1; days = 30 + days; }
	month = month + (endArr[3] - beginArr[3]);
	if (month < 0) { year = -1; month = 12 + month; }
	year = year + (endArr[1] - beginArr[1]);
	return year + "岁-" + month + "月-" + days + "天";
}
window.onload=function(){showdate();showsex();shownum();showprovinces();}
var YEAR = 1989,MONTH=5;